/*
 * Created on 2005-11-16
 *
 */
package com.yl.cap.analyst.report.chart.maker;

import java.awt.Color;
import java.io.FileOutputStream;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;

import com.yl.cap.analyst.report.chart.data.BarLineChartData;
import com.yl.cap.analyst.report.chart.data.ChartData;

public class BarMaker extends ChartMaker
{

    public void makeChart(String fileName, ChartData data) throws Exception
    {
        BarLineChartData chartData = (BarLineChartData) data;
        CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
                chartData.getRowKeys(), chartData.getColumnKeys(), chartData
                        .getData());
        //=======================================================
        JFreeChart chart = ChartFactory.createBarChart(data.getCaption(),
                chartData.getXCaption(), chartData.getYCaption(), dataset, PlotOrientation.VERTICAL, true, false,
                false);
        chart.setBackgroundPaint(Color.white);
        CategoryPlot categoryplot = chart.getCategoryPlot();
        categoryplot.setBackgroundPaint(Color.lightGray);
        categoryplot.setDomainGridlinePaint(Color.white);
        categoryplot.setRangeGridlinePaint(Color.white);
        NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        numberaxis.setUpperMargin(0.14999999999999999D);
        CategoryItemRenderer categoryitemrenderer = categoryplot.getRenderer();
        categoryitemrenderer.setSeriesItemLabelsVisible(0, Boolean.TRUE);
        CategoryAxis categoryaxis = categoryplot.getDomainAxis();
        categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
        //=============================================================
        FileOutputStream fos_jpg = null;
        try
        {
            fos_jpg = new FileOutputStream(fileName);
            ChartUtilities.writeChartAsJPEG(fos_jpg,100,chart,data.getWidth(),data.getHeight(),null);
            //ChartUtilities.writeChartAsJPEG(fos_jpg,100,chart,400,300,null);
        }
        catch (Exception e)
        {
            fos_jpg.close();
            throw e;
        }
        fos_jpg.close();
    }

}
